package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    /**
     * 菜品分页查询
     * @param queryDTO
     * @return
     */
    @Override
    public PageResult page(DishPageQueryDTO queryDTO) {
        PageHelper.startPage(queryDTO.getPage(),queryDTO.getPageSize());
        Page<DishVO> pages=dishMapper.page(queryDTO);
        long total = pages.getTotal();
        List<DishVO> records = pages.getResult();
        return new PageResult(total,records);
    }
    /**
     * 添加菜品
     * @param dishDTO
     */
    @Override
    @Transactional
    public void dish(DishDTO dishDTO) {
        Dish dishes = new Dish();
        BeanUtils.copyProperties(dishDTO,dishes);
        dishes.setStatus(StatusConstant.ENABLE);//设置状态
        long dishId = dishMapper.dish(dishes);//添加菜品并返回id
        //获取菜品的口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors!=null || flavors.size()!=0){
            flavors.forEach(flavor->{
                flavor.setDishId(dishId);
            });
        }
        //开始添加口味
        dishMapper.addFlavor(flavors);

    }

    /**
     * 根据id获取菜品信息
     * @param id
     * @return
     */
    @Override
    public DishVO getDishByID(long id) {
        //查询菜品的口味
        String category = dishMapper.selectCategoryId(id);
        //查询菜品分类
        List<DishFlavor> flavors = dishMapper.selectFlavorsById(id);
        DishVO dishVO = dishMapper.getDishByID(id);
        dishVO.setCategoryName(category);
        dishVO.setFlavors(flavors);
        return  dishVO;
    }

    /**
     * 修改菜品信息
     * @param dishDTO
     */
    @Override
    public void modify(DishDTO dishDTO) {
        //创建Dish对象
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.modify(dish);
    }
    /**
     * 菜品停售与启用
     * @param id
     */
    @Override
    public void stopOrStart(long status,long id) {
        dishMapper.stopOrStart(status,id);
    }

    /**
     * 批量删除菜品
     * @param ids
     */
    @Override
    @Transactional
    public void deleteDishes(List<Long> ids) {
        ids.forEach(id-> {
                    //1.根据id查找菜品 看状态是否为起售
                    DishVO dishVO = dishMapper.getDishByID(id);
                    if (dishVO.getStatus() == StatusConstant.ENABLE) {
                        //菜品正在起售 不支持删除
                        throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
                    }
                });
            //2.查看菜品是否关联套餐
            List<Long> results=setmealMapper.getSetmealIdsByDishIds(ids);
            if (results.size()!=0){
                //存在关联的套餐  不能删除
                throw  new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
            }
            ids.forEach(item->{
                //删除菜品
                dishMapper.deleteDishes(item);
                //删除菜品的口味
                categoryMapper.deleteById(item);
            });
        }
    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> list(Long categoryId) {
        Dish dish = new Dish();
        dish.setCategoryId(categoryId);
        return dishMapper.list(dish);
    }
}
